package com.example.vote.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.vote.bean.Result;
import com.example.vote.bean.User;
import com.example.vote.service.UserServiceImpl;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@RequestMapping("/user")
@Controller
@CrossOrigin
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Value("${vote_admin.account}")
    private String account;

    @Value("${vote_admin.password}")
    private String password;

    @PostMapping("/loginAdmin")
    @ResponseBody
    public Result loginAdmin(@RequestBody User loginUser) {
        HashMap<String, Object> map = new HashMap<>();
        if (account.equals(loginUser.getPhone()) && password.equals(loginUser.getPhone())) {
            StpUtil.login(loginUser.getPhone());
            StpUtil.getSession().set("user", loginUser);
            map.put("token", StpUtil.getTokenInfo().getTokenValue());
            return Result.success(map, "登录成功");
        } else {
            return Result.error("账号或密码错误");
        }

//        StpUtil.login(user.getPhone());
//        StpUtil.getSession().set("user", user);
////        map.put("phone", StpUtil.getLoginIdAsString());
//        map.put("token", StpUtil.getTokenInfo().getTokenValue());
//        return Result.success(map, "登录成功");
    }


    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User loginUser) {
        HashMap<String, Object> map = new HashMap<>();
        User user = userService.login(loginUser.getPhone(), loginUser.getName());
        if (!ObjectUtils.isEmpty(user)) {
            StpUtil.login(loginUser.getPhone());
            StpUtil.getSession().set("user", loginUser);
            map.put("token", StpUtil.getTokenInfo().getTokenValue());
            return Result.success(map, "登录成功");
        } else {
            return Result.error("账号或密码错误");
        }
//        StpUtil.login(user.getPhone());
//        StpUtil.getSession().set("user", user);
////        map.put("phone", StpUtil.getLoginIdAsString());
//        map.put("token", StpUtil.getTokenInfo().getTokenValue());
//        return Result.success(map, "登录成功");
    }


    @GetMapping("/logout")
    public void logout(HttpServletResponse response) throws IOException {
        StpUtil.logout();
        response.sendRedirect("/admin/page/login.html");
    }

    @PostMapping("/updatePhone")
    @ResponseBody
    public Result updatePhone(User user) {
        User one = userService.lambdaQuery().eq(User::getPhone, user.getPhone()).one();
        if (!ObjectUtils.isEmpty(one)) {
            return Result.error("该电话号码已被注册");
        }
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, user.getId()).set(User::getPhone, user.getPhone());
        boolean update = userService.update(updateWrapper);
        if (update) {
            return Result.success(null, "修改成功");
        }
        return Result.error("修改失败");
    }

}
